package com.java.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class MouseEventTest extends JFrame {
	private JLabel label;
	private JTextArea area;
	public MouseEventTest(){
		super("Mouse Event Test!");
		label=new JLabel("鼠标");
		label.setBackground(Color.BLACK);
		area=new JTextArea(10,20);
		JScrollPane jsp=new JScrollPane(area);
		this.add(label,BorderLayout.NORTH);
		this.add(jsp,BorderLayout.CENTER);
	}
	public void showMe(){
		addEventHandler();
		this.setSize(300, 200);
		setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void addEventHandler(){
		area.addMouseListener(new MouseAdapter(){

			public void mouseClicked(MouseEvent e) {
				String str=null;
				switch(e.getButton()){
				case MouseEvent.BUTTON1:str="左边按钮被点击";break;
				case MouseEvent.BUTTON2:str="中边按钮被点击";break;
				case MouseEvent.BUTTON3:str="右边按钮被点击";break;
				default:;
				}
				
				area.append(str+"("+e.getX()+","+e.getY()+")\n");
			}

			public void mouseEntered(MouseEvent e) {
				area.append("鼠标进入多行文本框\n");
			}

			public void mouseExited(MouseEvent e) {
				area.append("鼠标移出多行文本框\n");
			}

//			public void mousePressed(MouseEvent e) {
//				// TODO Auto-generated method stub
//				
//			}
//
//			public void mouseReleased(MouseEvent e) {
//				// TODO Auto-generated method stub
//				
//			}
			
		});
		area.addMouseMotionListener(new MouseMotionListener(){

			public void mouseDragged(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			public void mouseMoved(MouseEvent e) {
				label.setText("鼠标指针坐标("+e.getX()+","+e.getY()+")");
			}
			
		});
	}
	public static void main(String[] args){
		new MouseEventTest().showMe();
	}
}
